package com.example.psychologicalappointmentsystem.service.impl.alipay;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.psychologicalappointmentsystem.Object.alipayVO;
import com.example.psychologicalappointmentsystem.config.AlipayConfig;
import com.example.psychologicalappointmentsystem.service.AliPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class AlipayServiceImpl implements AliPayService {
    public String pay(alipayVO alipayVO) {

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        JSONObject jsonObject = JSONUtil.createObj()
                .set("out_trade_no", alipayVO.getTrade_no())
                .set("total_amount", alipayVO.getTotal_amount())
                .set("subject", alipayVO.getSubject())
                .set("body", alipayVO.getDescription())
                .set("product_code", "FAST_INSTANT_TRADE_PAY")
                .set("timeout_express", "1m");

        alipayRequest.setBizContent(jsonObject.toString());

        String result = "";
        try {
            //请求
            result = alipayClient.pageExecute(alipayRequest).getBody();
            log.info("调用ali支付返回结果->{}",result);
        } catch (Exception e) {
            log.error("调用ali支付异常,{}",e.getMessage());
        }
        return result;
    }

    @Override
    public String tradeQuery() {
        return null;
    }

    @Override
    public String tradeRefund() {
        return null;
    }
}
